package org.liaohailong.victorlibrary.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.jetbrains.annotations.Nullable;
import org.liaohailong.library.widget.ShapeImageView;
import org.liaohailong.victorlibrary.R;
import org.liaohailong.victorlibrary.base.BaseActivity;

/**
 * Author: liaohailong
 * Date: 2019/5/7
 * Time: 21:16
 * Description: 演示ShapeImageView
 **/
public class ShapeImageActivity extends BaseActivity {
    public static void show(Context context) {
        Intent intent = new Intent(context, ShapeImageActivity.class);
        context.startActivity(intent);
    }

    @ShapeImageView.Type
    private int shapeType;

    private ImageView.ScaleType scaleType;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shape_image);

        final ShapeImageView roundRectImg = findViewById(R.id.shape_image_view);

        if (roundRectImg != null) {
            roundRectImg.setImageResource(R.drawable.timg);
            shapeType = roundRectImg.getShapeType();
            scaleType = roundRectImg.getScaleType();
        }

        // 修改显示图形
        findViewById(R.id.shape_btn).setOnClickListener(new View.OnClickListener() {

            private int index = 0;
            private int[] types = {ShapeImageView.Type.RECT, ShapeImageView.Type.CIRCLE, ShapeImageView.Type.ROUND_RECT};

            @Override
            public void onClick(View v) {
                if (roundRectImg == null) {
                    return;
                }
                index++;
                shapeType = types[index % types.length];
                roundRectImg.setShapeType(shapeType);
                showStatusText();
            }
        });
        // 修改裁剪类型
        findViewById(R.id.scale_type_btn).setOnClickListener(new View.OnClickListener() {
            private int index = 0;
            private ImageView.ScaleType[] types = {
                    ImageView.ScaleType.MATRIX,
                    ImageView.ScaleType.FIT_XY,
                    ImageView.ScaleType.FIT_START,
                    ImageView.ScaleType.FIT_CENTER,
                    ImageView.ScaleType.FIT_END,
                    ImageView.ScaleType.CENTER,
                    ImageView.ScaleType.CENTER_CROP,
                    ImageView.ScaleType.CENTER_INSIDE
            };

            @Override
            public void onClick(View v) {
                if (roundRectImg == null) {
                    return;
                }
                index++;
                scaleType = types[index % types.length];
                roundRectImg.setScaleType(scaleType);
                showStatusText();
            }
        });
    }

    private void showStatusText() {
        TextView statusTxt = findViewById(R.id.status_txt);
        String shapeText = "";
        switch (shapeType) {
            case ShapeImageView.Type.CIRCLE:
                shapeText = "圆形";
                break;
            case ShapeImageView.Type.RECT:
                shapeText = "矩形";
                break;
            case ShapeImageView.Type.ROUND_RECT:
                shapeText = "圆角矩形";
                break;
            default:
                break;
        }

        String scaleText = "";
        switch (scaleType) {
            case MATRIX:
                scaleText = "MATRIX";
                break;
            case FIT_XY:
                scaleText = "FIT_XY";
                break;
            case FIT_START:
                scaleText = "FIT_START";
                break;
            case FIT_CENTER:
                scaleText = "FIT_CENTER";
                break;
            case FIT_END:
                scaleText = "FIT_END";
                break;
            case CENTER:
                scaleText = "CENTER";
                break;
            case CENTER_CROP:
                scaleText = "CENTER_CROP";
                break;
            case CENTER_INSIDE:
                scaleText = "CENTER_INSIDE";
                break;
            default:
                break;
        }
        statusTxt.setText("图片图形为：" + shapeText + "，裁剪类型为：" + scaleText);
    }
}
